/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amuthu.indicator;

import org.amuthu.DataStore;
import org.amuthu.TalibMgr;

/**
 *
 * @author prabhu
 */
public class MACD extends Indicator
{
   private int fastPeriod;
   private int slowPeriod;
   private int signalPeriod;
   private  double signal[];
   private  double trigger[];
   private double macd[];

   /**
    *
    * @param dataStore
    * @param fastPeriod
    * @param slowPeriod
    * @param signalPeriod
    * @param startOffset
    */
   public MACD(DataStore dataStore, int fastPeriod, int slowPeriod, int signalPeriod, int startOffset)
   {
      super(dataStore, startOffset);
      this.fastPeriod = fastPeriod;
      this.slowPeriod = slowPeriod;
      this.signalPeriod = signalPeriod;

      signal = new double[1];
      trigger = new double[1];
      macd = new double[1];
   }

   /**
    *
    * @return
    */
   @Override
   public double calculate()
   {
      int lastIdx = getLastIndex();

      TalibMgr.getTalib().macd(lastIdx,
                               lastIdx,
                               getDataStore().getClose(),
                               fastPeriod,
                               slowPeriod,
                               signalPeriod,
                               TalibMgr.getOutBeginIdx(),
                               TalibMgr.getOutNbElement(),
                               macd,
                               signal,
                               trigger);

      return getMacd();
   }

   public double getMacd()
   {
      return roundDouble(macd[0], 2);
   }

   public double getSignal()
   {
      return roundDouble(signal[0], 2);
   }

   public double getTrigger()
   {
      return roundDouble(trigger[0], 2);
   }

   @Override
   public int getDayDependency()
   {
      return TalibMgr.getTalib().macdLookback(fastPeriod, slowPeriod, signalPeriod) + startOffset;
   }

}
